package com.ruoyi.base.config.redisson;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * redisson
 *
 * @author lcf
 */
@Data
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedissonConfig {
    private String host;
    private String password;
    private String port;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        String host = "redis://" + this.host + ":" + port;

        config.useSingleServer()
                .setAddress(host)
                .setPassword(StringUtils.isBlank(password) ? null : password)
                .setTimeout(5000)
                .setConnectTimeout(10000)
                .setRetryAttempts(3)
                .setRetryInterval(1000);

        return Redisson.create(config);
    }
}
